﻿#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    public static class SelectionUtility {

        /// <summary>
        /// Returns true if an object with the given type is selected.
        /// </summary>
        public static bool IsObjectOfTypeSelected<T> (out T obj) where T : UnityEngine.Object {
            UnityEngine.Object selectedObject = Selection.activeObject;

            if (selectedObject.IsTypeOf<T> ()) {
                obj = selectedObject as T;
                return true;
            }

            if (typeof (T) == typeof (Component) || typeof (T).IsSubclassOf (typeof (Component))) {
                if (selectedObject.IsTypeOf<GameObject> ()) {
                    GameObject gameObject = selectedObject as GameObject;
                    if (gameObject != null) {
                        obj = gameObject.GetComponent<T> ();
                        return obj != null;
                    }
                }
            }

            obj = null;
            return false;
        }

    }


}
#endif